package com.game;

import android.content.Intent;
import android.database.Cursor;
import android.widget.Button;
import android.widget.EditText;

public class EditGameActivity extends EditTableActivity {

	private EditText gameNameEditText;
	private EditText gameDescEditText;
	private EditText gameInstrEditText;

	@Override
	protected void initalize(){
		setContentView(R.layout.editgame);
        SubmitButton = (Button)findViewById(R.id.g_SubmitButton);
        SubmitButton.setOnClickListener(this);
        ViewButton = (Button)findViewById(R.id.g_viewGameButton);
        ViewButton.setOnClickListener(this);
        HomeButton = (Button)findViewById(R.id.g_homeButton);
        HomeButton.setOnClickListener(this);
        EditButton = (Button)findViewById(R.id.g_editGameButton);
        EditButton.setOnClickListener(this);
        
        
		gameNameEditText = (EditText)findViewById(R.id.g_gameNameEditText);
		gameDescEditText = (EditText)findViewById(R.id.g_gameDescEditText);
		gameInstrEditText = (EditText)findViewById(R.id.g_gameInstrEditText);
		
		ViewClass = ViewGameActivity.class;

	}
	
	@Override
    protected void save(){

    	String gameName = gameNameEditText.getText().toString();
    	String gameDesc = gameDescEditText.getText().toString();
    	String gameInstr = gameInstrEditText.getText().toString();
    	Game newGame = new Game(gameName,gameDesc,gameInstr);
    	myDBAdapter.insertGameData(newGame);
    	view();
        
    }
	
	@Override
	protected void edit() {
		startActivityForResult(new Intent(this,ListGameActivity.class),0);
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{  
		super.onActivityResult(requestCode, resultCode, intent);
		switch(requestCode)
		{
		case 0:
			
			if (resultCode == RESULT_OK && intent.hasExtra("ItemId"))
			{
			int _id = intent.getIntExtra("ItemId", -1);
			Cursor cursor = myDBAdapter.getGameByIdCursor(_id);
			cursor.moveToFirst();
			gameNameEditText.setText(cursor.getString(cursor.getColumnIndex("game_name")));
			gameDescEditText.setText(cursor.getString(cursor.getColumnIndex("game_desc")));
			gameInstrEditText.setText(cursor.getString(cursor.getColumnIndex("game_instr")));
			}
			break;
		}
	}
	
	
	
	@Override
	public void onStart(){
		super.onResume();	
		gameNameEditText.setText("");
		gameDescEditText.setText("");
		gameInstrEditText.setText("");
	}
	
	
	
}
